package org.jackysoft.struts2.business;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.Months;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jackysoft.entity.AbstractReporter;
import org.jackysoft.entity.Department;
import org.jackysoft.ireporter.IReporterTool;
import org.jackysoft.struts2.UtilsCRUDAction;

import com.google.common.collect.Maps;

@Results({ @Result(name = "jasper-html", type = "jasper", params = {
		"location", "/jasper/report1.jasper", "format", "HTML", "dataSource",
		"jrDataSource" }) })
public class ReporterCRUDAction<R extends AbstractReporter> extends UtilsCRUDAction<R> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6171149105351855926L;
	
	protected JRDataSource jrDataSource;
	protected Department department;
	protected String catalog;
	protected DateTime startDate;	
	protected DateTime endDate;
	protected boolean finished = true;
	protected int year;
	protected int month;
	protected int date;
	
	@Override
	public String list() throws Exception {
		return LIST;
	}

	@Override
	public String start() throws Exception {
		return START;
	}

	@Override
	public String add() throws Exception {
		return ADD;
	}
	
	@Override
	public String create() throws Exception {	
		return CREATE;
	}

	@Override
	public String remove() throws Exception {
		return REMOVE;
	}

	@Override
	public String update() throws Exception {
		return "update";
	}

	@Override
	public String get() throws Exception {
		return "get";
	}

	@Override
	public String gets() throws Exception {
		return "gets";
	}

	
	public String preselect() throws Exception{
		return "preselect";
				
	}
	
	public String jasperhtml() throws Exception {
		  return "jasper-html";
	}
	
	
		

	@Action(value = "/reporter/compile", results = { @Result(name = "success", type = "dispatcher", params = {
			"location", "/reporter/compile/index-success.jsp" }) })
	public String compileReport() {
		File f = new File(ServletActionContext.getServletContext().getRealPath(
				"/jasper/"));
		IReporterTool.compileReport(f);

		return SUCCESS;
	}

	public JRDataSource getJrDataSource() throws JRException {
		return jrDataSource;
	}

	public void setJrDataSource(JRDataSource jrDataSource) {
		this.jrDataSource = jrDataSource;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public String getCatalog() {
		return catalog;
	}

	public void setCatalog(String catalog) {
		this.catalog = catalog;
	}

	public DateTime getStartDate() {
		return startDate;
	}

	public void setStartDate(DateTime startDate) {
		this.startDate = startDate;
	}

	public DateTime getEndDate() {
		return endDate;
	}

	public void setEndDate(DateTime endDate) {
		this.endDate = endDate;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDate() {
		return date;
	}

	public void setDate(int date) {
		this.date = date;
	}

	public boolean isFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}	
}
